package com.glsc.ngateway.opmanage.service.rule;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.glsc.ngateway.common.api.common.utils.DateUtil;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.rule.RuleFileInfo;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.rule.RuleRepository;
import com.glsc.ngateway.opmanage.utils.DateUtils;

import static com.glsc.ngateway.opmanage.utils.DateUtils.formatDate_;

/**
 * @author houzhc
 * @description: TODO hzc
 * @date 2024年08月07日
 */
@Service
public class RuleService {

    @Autowired
    private RuleRepository ruleRepository;

    public void save(RuleFileInfo ruleFileInfo) {
        ruleFileInfo.setCreateTime(LocalDateTime.now());
        ruleFileInfo.setCreateDate(DateUtils.getCurrentDate(formatDate_));
        ruleRepository.save(ruleFileInfo);
    }

    public void deleteById(Integer id) {
        ruleRepository.deleteById(id);
    }

    public Page<RuleFileInfo> page(Specification spec, Integer pageNo, Integer pageSize) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        return ruleRepository.findAll(spec, pageable);
    }
}
